package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.itboye.textileexpes.beans.NewMeesgeBeans;
import com.itboye.textileexpes.presenter.MainCategoryTypeTwoPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.EnumUtility;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.widget.XListView;
import com.itboye.textileexpres.widget.XListView.IXListViewListener;

public class FragmentNews extends BaseFragment implements Observer,
		IXListViewListener {
	XListView list;
	AdapterCustomerService adapterCustomerService;
	MainCategoryTypeTwoPresenter presenter;
	List<NewMeesgeBeans> beans;
	int page_index = 1;
	int page_size = 10;
	LinearLayout linelyout;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_news;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		presenter = new MainCategoryTypeTwoPresenter(this);
		beans = new ArrayList<NewMeesgeBeans>();
		presenter.onMesssge(IsMobileNumPhone.isUid(),
				EnumUtility.IsMessge.getValue() + "", page_index + "",
				page_size + "");
		list.setPullLoadEnable(true);
		list.setPullRefreshEnable(true);
		list.setXListViewListener(this);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						ActivityCustomerDetail.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == presenter.Messge_success) {
				beans = (List<NewMeesgeBeans>) entity.getData();
				if (beans == null || beans.size() <= 0) {
					linelyout.setVisibility(View.VISIBLE);
					list.setVisibility(View.GONE);
				} else {
					linelyout.setVisibility(View.GONE);
					list.setVisibility(View.VISIBLE);
					adapterCustomerService = new AdapterCustomerService(
							getActivity(), beans);
					list.setAdapter(adapterCustomerService);
				}

			}
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page_size = 10;
		presenter.onMesssge(IsMobileNumPhone.isUid(),
				EnumUtility.IsMessge.getValue() + "", page_index + "",
				page_size + "");
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		page_size++;
		presenter.onMesssge(IsMobileNumPhone.isUid(),
				EnumUtility.IsMessge.getValue() + "", page_index + "",
				page_size + "");
	}

}
